package com.ants.platform.core.utils;

import com.ants.platform.core.bean.UserToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获取token 解密token
 * 先加密base64 在进行des3加密
 * 根据用户ID 用户账号 时间戳加密
 * Created by zhituliu on 2016/3/27.
 */
public class TokenUtil {

    private static final Logger logger = LoggerFactory.getLogger(TokenUtil.class);

    /**
     * 账号密码生成token信息
     *
     * @param token
     * @return
     */
    public static String getToken(UserToken token) {
        try {
            return StringEncryptUtil.encode(token.getUserId() + "_" + token.getUserName() + "_" + token.getTimeSpan());
        } catch (Exception e) {
            logger.error("获取加密token令牌失败!", e.getMessage());
            return null;
        }

    }

    /**
     * 获取token信息
     *
     * @param token
     * @return
     */
    public static UserToken decodeToken(String token) {
        try {
            String userinfo = StringEncryptUtil.decode(token);
            String[] str = userinfo.split("_");
            return new UserToken(str[0], str[1], Long.parseLong(str[2]));
        } catch (Exception e) {
            logger.error("解密token令牌失败!", e.getMessage());
            return null;
        }
    }
}
